package com.netease.xone.widget;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

public class NonBlockScrollView1 extends ScrollView {
	private static boolean DEBUG = true;
	private static final String TAG = NonBlockScrollView1.class.getSimpleName();
	private final ArrayList<View> mViewChildren = new ArrayList<View>();
	private final HashMap<View, Rect> mViewGlobalVisibleRect = new HashMap<View, Rect>();

	public NonBlockScrollView1(Context context) {
        super(context);
    }
    public NonBlockScrollView1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NonBlockScrollView1(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    	boolean bRes = false;
    	
    	do {
    		switch (ev.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				bRes = computeChildrenAndLocationOnScreen();
				break;
			case MotionEvent.ACTION_MOVE:
				break;
			case MotionEvent.ACTION_UP:
				break;
			case MotionEvent.ACTION_CANCEL:
				break;
			default:
				break;
			}
    		if (!bRes) {
    			bRes = super.onInterceptTouchEvent(ev);
    		}
    	} while(false);
    	
    	if (DEBUG) {
			switch (ev.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				Log.d(TAG, "onInterceptTouchEvent MotionEvent.ACTION_DOWN" + " bRes=" + bRes);
				break;
			case MotionEvent.ACTION_MOVE:
				Log.d(TAG, "onInterceptTouchEvent MotionEvent.ACTION_MOVE" + " bRes=" + bRes);
				break;
			case MotionEvent.ACTION_UP:
				Log.d(TAG, "onInterceptTouchEvent MotionEvent.ACTION_UP" + " bRes=" + bRes);
				break;
			case MotionEvent.ACTION_CANCEL:
				Log.d(TAG, "onInterceptTouchEvent MotionEvent.ACTION_CANCEL" + " bRes=" + bRes);
				break;
			default:
				break;
			}
		}
    	return bRes;
    }
    
    private boolean bLastbRes = false;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    	boolean bRes = false;
    	
    	do {
    		switch (ev.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				break;
			case MotionEvent.ACTION_MOVE:
				bRes = computeChildrenAndLocationOnScreen();
				if (false == bRes && true == bLastbRes) {
					ev.setAction(MotionEvent.ACTION_DOWN);
					dispatchTouchEvent(ev);
				}
				
				bLastbRes = bRes;
				break;
			case MotionEvent.ACTION_UP:
				break;
			case MotionEvent.ACTION_CANCEL:
				break;
			default:
				break;
			}
    		bRes = super.onTouchEvent(ev);
    	} while(false);
    	
    	if (DEBUG) {
			switch (ev.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				Log.d(TAG, "onTouchEvent MotionEvent.ACTION_DOWN" + " bRes=" + bRes);
				break;
			case MotionEvent.ACTION_MOVE:
				Log.d(TAG, "onTouchEvent MotionEvent.ACTION_MOVE" + " bRes=" + bRes);
				break;
			case MotionEvent.ACTION_UP:
				Log.d(TAG, "onTouchEvent MotionEvent.ACTION_UP" + " bRes=" + bRes);
				break;
			case MotionEvent.ACTION_CANCEL:
				Log.d(TAG, "onTouchEvent MotionEvent.ACTION_CANCEL" + " bRes=" + bRes);
				break;
			default:
				break;
			}
		}
    	
    	return bRes;
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    	super.onLayout(changed, l, t, r, b);
    	getChildrenAndLocationOnScreen(this);
    }
    
    private void getChildrenAndLocationOnScreen(ViewGroup root) {
    	ViewGroup vg = (ViewGroup)root.getChildAt(0);
    	View view;
    	Rect rect;
    	final int count = vg.getChildCount();
    	
    	for(int index = 0; index < count; index++) {
    		view = vg.getChildAt(index);
    		rect = new Rect();
    		view.getGlobalVisibleRect(rect);
    		if (!mViewChildren.contains(view)) {
    			mViewChildren.add(view);
    		}
    		mViewGlobalVisibleRect.put(view, rect);
    		if (DEBUG) {
    			Log.d(TAG, "index="+ index + " " + view.toString());
    			Log.d(TAG, "GlobalVisibleRect=" + rect.toString());
    			Log.d(TAG, "\n");
    		}
    	}
    }
    
    private boolean computeChildrenAndLocationOnScreen() {
    	boolean bRes = false;
    	View child = mViewChildren.get(0);
    	
    	do {
    		Rect rect = new Rect();
    		child.getGlobalVisibleRect(rect);
    		Rect rt = mViewGlobalVisibleRect.get(child);
    		
    		if (rect.top == rt.top) {
    			bRes = true;
    		}
    		
    		if (DEBUG) {
    			Log.d(TAG, "GlobalVisibleRect=" + rect.toString() + "  bRes=" + bRes);
    		}
    		
    	} while (false);
    	
    	return bRes;
    }
}
